import Mock from 'mockjs'

const Random = Mock.Random
const getQueryVariable = function (url, variable) {
  const index = url.indexOf('?')
  const query = url.substring(index + 1)
  var vars = query.split('&')
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split('=')
    if (pair[0] === variable) { return pair[1] }
  }
  return (false)
}
const list1 = []
const getList1 = function () {
  for (let i = 0; i < 278; i++) {
    let newList = { // 详细 规则 参照mockjs官网
      order: Random.string('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 16, 16),
      name: Random.cname(), // Random.cname() 随机生成中文名
      payTime: Random.date() + ' ' + Random.time(), // Random.date()指示生成的日期字符串的格式,默认为yyyy-MM-dd；Random.time() 返回一个随机的时间字符串
      payType: Random.integer(1, 2),
      paySum: '40.00',
      address: Random.county(true)
    }
    list1.push(newList)
  }
}
const listData1 = function (options) {
  const total = 278
  const pageSize = Number(getQueryVariable(options.url, 'pageSize'))
  const pageNum = Number(getQueryVariable(options.url, 'pageNum'))
  let maxI = pageSize
  if (pageNum * pageSize >= total) {
    maxI = total - (pageNum - 1) * pageSize
  }
  let _list = list1.slice((pageNum - 1) * pageSize, (pageNum - 1) * pageSize + maxI)
  return {
    pageNum,
    pageSize,
    total,
    list: _list
  }
}

getList1()
// url为要拦截的请求地址  请求方式  请求数据（规则） （此处api会被mockjs拦截）
Mock.mock(RegExp('http://www.mock.com/listData1.*'), 'get', listData1)
